<?php
namespace App\DBs;

use App\DBs\BaseDB;
use Illuminate\Support\Facades\DB;

class MoreDB extends BaseDB
{
    /**
     * MORE
     * name 应用名称
     * admin 管理员UID
     * img 图标
     * link 链接地址
     */

    protected $table = 'more';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表
     */
    public function getIndex($statuss=array())
    {
        $models = DB::table($this->table)->orderBy('id','desc')->get();
        $dataArr = array();
        if (!count($models)) {
            return array('code'=>-1, 'msg'=>'没有数据');
        }
        foreach ($models as $model) {
            $createTime = $model->created_at ? date('Y-m-d',$model->created_at) : '未定义';
            $dataArr[] = [
                'id' => $model->id,
                'name' => $model->name,
                'img' => $model->img,
                'link' => $model->link,
                'createTime' => $createTime,
            ];
        }
        return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
    }

    /**
     * 详情
     */
    public function getOne($bugId)
    {
        $model = DB::table($this->table)->where('id',$bugId)
            ->orderBy('id','desc')->first();
        if (!$model) {
            return array('code'=>-1, 'msg'=>'没有数据');
        }
        $statuss = $this->getStatuss();
        $genres = $this->getGenres();
        $publish = $this->getAdminName($model->admin);
        $almName = $this->getAdminName($model->alm);
        $statusName = array_key_exists($model->status,$statuss) ?
            $statuss[$model->status] : '未定义';
        $genreName = array_key_exists($model->genre,$genres) ?
            $genres[$model->genre] : '未定义';
        $bugDetail = $model->detail ? $model->detail : '未定义';
        $createTime = $model->created_at ? date('Y-m-d',$model->created_at) : '未定义';
        $dealTimeStr = $model->dealTime ? date('Y-m-d',$model->dealTime) : '未完成';
        $dataArr = [
            'bugId' => $model->id,
            'bugName' => $model->name,
            'bugLink' => $model->link,
            'bugDetail' => $bugDetail,
            'admin' => $model->admin,
            'adminName' => $publish,
            'alm' => $model->alm,
            'almName' => $almName,
            'genreName' => $genreName,
            'statusName' => $statusName,
            'createTime' => $createTime,
            'dealTime' => $dealTimeStr,
        ];
        //问题图片
        $dataArr['imgCount'] = $this->isImg($model->admin,1,$model->id);
        return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
    }

    /**
     * 新增
     */
    public function setAdd($admin,$alm,$genre,$name,$link,$detail)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->table)->insertGetId([
                'name' => $name,
                'link' => $link,
                'detail' => $detail,
                'admin' => $admin,
                'alm' => $alm,
                'genre' => $genre,
                'created_at' => time(),
            ]);
            $dataArr = array(
                'proId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }
}